#!/bin/bash

# ******************************************
# @author: LANSHANQUAN
# @date: 2021/07/20
# @modify: 2023/12/17
# @function: Operation App easily by myself!
# ******************************************

ZK_CLIENT_PORT=16608
ZK_CLUSTER_PORT=16609
ZK_ELECT_PORT=16610

KAFKA_PORT=16611

ROCKETMQ_PORT=16612

SPARK_PORT=16614
SPARK_PORT=16615
SPARK_PORT=16616

POSTGRESQL_PORT=16619

BASE_DIR="/cloud"
DB_DIR="/cloud/db"
APP_DIR="/cloud/apps"

ZOOKEEPER_HOME="${APP_DIR}/zookeeper"

KAFKA_HOME="${APP_DIR}/kafka"
ROCKETMQ_HOME="${APP_DIR}/rocketmq"
SPARK_HOME="${APP_DIR}/spark"
HADOOP_HOME="${APP_DIR}/hadoop"
HIVE_HOME="${APP_DIR}/hive"

APP_RUNNING=10001
APP_STOPPED=10002

BACKUP_IP="172.16.16.2"
CLOUD1_IP="172.16.0.17"
CLOUD2_IP="172.16.16.13"
CLOUD3_IP="172.16.16.4"

BACKUP_IP_EXT="42.193.157.58"
CLOUD1_IP_EXT="106.52.111.18"
CLOUD2_IP_EXT="81.71.73.65"
CLOUD3_IP_EXT="134.175.222.193"

function checkUser()
{
    echo "[Function] check user!"
    username=`whoami`
    if [[ ! "${username}"x == "ituser"x ]];then
        echo "[INFO] pls use ituser to start|stop app!"
        return 1
    fi
}

function usage()
{
    echo "usage: bash AppUtil.sh AppName ActionName"
    #echo "    eg: bash AppUtil.sh zookeeper start"
}

function startZookeeper()
{
    echo "[Function] Start Zookeeper now!"
    . /home/ituser/.bashrc
    . /etc/profile
    cd $ZOOKEEPER_HOME/bin
    bash zkServer.sh start
    sleep 3
    #lsof -i:$ZOOKEEPER_PORT
    lsof -i:${ZK_CLIENT_PORT} > /dev/null 2>&1
    if [[ $? -eq 0 ]];then
        echo "Start Zookeeper success!"
    else
        echo "Start Zookeeper failed!"
    fi
}

function startKafka()
{
    echo "[Function] Start Kafka now!"
    #zkPid=`ps -ef|grep zookeeper|grep -v grep|awk '{print $2}'`
    #if [[ -z "${zkPid}" ]];then
    #    echo "[ERROR] zookeeper is not running, please check zookeeper first!"
    #	return 1
    #fi
    lsof -i:${KAFKA_PORT} >/dev/null 2>&1
    if [[ $? -eq 0 ]];then
        echo "[INFO] kafka is already running!"
    	return 1
    fi
    ${KAFKA_HOME}/bin/kafka-server-start.sh -daemon ${KAFKA_HOME}/config/server.properties

    kafkaPid=`ps -ef | grep kafka | grep -v grep | awk '{print $2}'`
    if [[ -z "${kafkaPid}" ]];then
        echo "[ERROR] kafka is not running, start failed!"
	return 1
    fi
    echo "[INFO] kafka is running!"
    return 0
}

function startHadoop()
{
    echo "[Function] Start Hadoop now!"
}

function startSpark()
{
    echo "[Function] Start Saprk now!"
}

function startRabbitMQ()
{
    echo "[Function] Start RabbitMQ now!"
}

function startRocketMQ()
{
    echo "[Function] Start RocketMQ now!"
    lsof -i:${ROCKETMQ_PORT}
    if [[ $? -eq 0 ]];then
        echo "[WARN] RocketMQ is running!"
    	return 1
    fi

    cloud_host_ip=${BACKUP_IP_EXT}

    cloud_host_name=`hostname`

    if [[ ${cloud_host_name} == "cloud1" ]];then
        cloud_host_ip=${CLOUD1_IP_EXT}
    elif [[ ${cloud_host_name} == "cloud2" ]];then
        cloud_host_ip=${CLOUD2_IP_EXT}
    elif [[ ${cloud_host_name} == "cloud3" ]];then
        cloud_host_ip=${CLOUD3_IP_EXT}
    else
        echo "[WARN] hostname error!"
	return 1
    fi

    nohup ${ROCKETMQ_HOME}/bin/mqnamesrv -c ${ROCKETMQ_HOME}/conf/namesrv.properties &

    sleep 1

    nohup ${ROCKETMQ_HOME}/bin/mqbroker -n ${cloud_host_ip}:${ROCKETMQ_PORT} -c ${ROCKETMQ_HOME}/conf/broker.conf autoCreateTopicEnable=true &

    sleep 3

    lsof -i:${ROCKETMQ_PORT}
    if [[ $? -eq 0 ]];then
        echo "[WARN] RocketMQ is running!"
    fi

}

function stopZookeeper()
{
    echo "[Function] Stop zookeeper now!"
    . /home/ituser/.bashrc
    . /etc/profile
    cd $ZOOKEEPER_HOME/bin
    bash zkServer.sh stop
    sleep 3
    lsof -i:${ZK_CLIENT_PORT} > /dev/null 2>&1
    if [[ $? -ne 0 ]];then
        echo "Stop Zookeeper success!"
    else
        echo "Stop Zookeeper failed!"
    fi
}

function stopKafka()
{
    echo "[Function] Stop Kafka now!"
    #kafkaPid=`ps -ef | grep kafka | grep -v grep | awk '{printf "%s", $2}'`
   # echo "xxx"${kafkaPid}"YYY"
    #if [[ -z "${kafkaPid}" ]];then
  #  if [[ x"$kafkaPid" = x ]];then
 #       echo "[INFO] kafka is not running!"
#	return 0
    #fi
    lsof -i:${KAFKA_PORT} >/dev/null 2>&1
    if [[ $? -ne 0 ]];then
        echo "[INFO] kafka is not running!"
    	return 1
    fi
    ${KAFKA_HOME}/bin/kafka-server-stop.sh
    sleep 3
    lsof -i:${KAFKA_PORT} >/dev/null 2>&1
    if [[ $? -ne 0 ]];then
        echo "[INFO] kafka is stopped success!"
    	return 0
    fi
}

function stopHadoop()
{
    echo "[Function] Stop Hadoop now!"
}

function stopSpark()
{
    echo "[Function] Stop Spark now!"
}

function stopRabbitMQ()
{
    echo "[Function] Stop RabbitMQ now!"
}

function stopRocketMQ()
{
    echo "[Function] Stop RocketMQ now!"

    sh ${ROCKETMQ_HOME}/bin/mqshutdown broker

    sleep 3

    sh ${ROCKETMQ_HOME}/bin/mqshutdown namesrv
}

function statusZookeeper()
{
    echo "[Function] Check Zookeeper status now!"
    lsof -i:${ZK_CLIENT_PORT}
    if [[ $? -eq 0 ]];then
        return ${APP_RUNNING}
    else
        return ${APP_STOPPED}
    fi
}

function statusKafka()
{
    echo "[Function] Check Kafka status now!"
    #kafkaPid=`ps -ef | grep kafka | grep -v grep | awk '{print $2}'`
    #if [[ -z "${kafkaPid}" ]];then
    #    echo "[INFO] kafka is not running!"
    #	return 1
    #fi
    lsof -i:${KAFKA_PORT}
    if [[ $? -ne 0 ]];then
        echo "[WARN] kafka is not running!"
    	return 1
    fi
    echo "[INFO] kafka is running!"
}

function statusHadoop()
{
    echo "[Function] Check Hadoop status now!"
}

function statusSpark()
{
    echo "[Function] Check Saprk status now!"
}

function statusRabbitMQ()
{
    echo "[Function] Check RabbitMQ status now!"
}

function statusRocketMQ()
{
    echo "[Function] Check RocketMQ status now!"

    lsof -i:${ROCKETMQ_PORT}
    if [[ $? -ne 0 ]];then
        echo "[WARN] RocketMQ is not running!"
    	return 1
    fi
    echo "[INFO] RocketMQ is running!"
}

function restartZookeeper()
{
    echo "[Function] Restart Zookeeper now!"
    stopZookeeper
    startZookeeper
}

function restartKafka()
{
    echo "[Function] Restart Kafka now!"
}

function restartHadoop()
{
    echo "[Function] Restart Hadoop now!"
}

function restartSpark()
{
    echo "[Function] Restart Spark now!"
}

function restartRabbitMQ()
{
    echo "[Function] Restart RabbitMQ now!"
}

function restartRocketMQ()
{
    echo "[Function] Restart RocketMQ now!"
    stopRocketMQ
    startRocketMQ
}

function usageZookeeper()
{
    echo "[Function] Usage of Zookeeper!"
}

function usageKafka()
{
    echo "[Function] Usage of Kafka!"
    cat << EOF
[Operation]
cd /cloud/apps/kafka/bin
./kafka-topics.sh --create --zookeeper 106.52.111.18:16608,81.71.73.65:16608,134.175.222.193:16608 --replication-factor 3 --partitions 2  --topic learn-kafka-topic

./kafka-topics.sh  --describe --zookeeper 106.52.111.18:16608,81.71.73.65:16608,134.175.222.193:16608 --topic learn-kafka-topic

[Configution]
/cloud/apps/kafka/config/server.properties

[Log]
/cloud/apps/kafka/log

[Comment]
Kafka consumer group get only one partition message

[ZK]
Kafka partition state /brokers/topics/learn-kafka-topic/partitions/0/state

EOF

}

function usageHadoop()
{
    echo "[Function] Usage of Hadoop!"
    cat << EOF

EOF
}

function usageSpark()
{
    echo "[Function] Usage of Spark!"
    cat << EOF

EOF
}

function usageRabbitMQ()
{
    echo "[Function] Usage of RabbitMQ!"
    cat << EOF

EOF
}

function usageRocketMQ()
{
    echo "[Function] Usage of RocketMQ!"
    cat << EOF

EOF
}

function fn_main()
{  
    if [[ ! $# -eq 2 ]];
    then
        usage
        exit
    fi

    appName=$1
    appAction=$2

    checkUser || return $?

    if [[ "${appName}"x == "zookeeper"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startZookeeper || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopZookeeper || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartZookeeper || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusZookeeper || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageZookeeper
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 

    if [[ "${appName}"x == "kafka"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startKafka || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopKafka || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartKafka || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusKafka || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageKafka
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 

    if [[ "${appName}"x == "rocketmq"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startRocketMQ || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopRocketMQ || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartRocketMQ || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusRocketMQ || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageRocketMQ
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 

    if [[ "${appName}"x == "hadoop"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startHadoop || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopHadoop || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartHadoop || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusHadoop || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageHadoop
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 

    if [[ "${appName}"x == "spark"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startSpark || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopSpark || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartSpark || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusSpark || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageSpark
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 

    if [[ "${appName}"x == "rabbitmq"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startRabbitMQ || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopRabbitMQ || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartRabbitMQ || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusRabbitMQ || return $?
        elif [[ "${appAction}"x == "usage"x ]];then
            usageRabbitMQ
        else
	    echo "[ERROR] Parameter error!" && return 1
        fi
    fi 
}

fn_main "$@"
result=$?

if [[ "$result" -eq 0 ]]
then
    echo "SUCCESS"
else
    echo "FAILED"
fi
